package io.renren.modules.photography.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.exception.RRException;
import io.renren.modules.photography.entity.PWorksEntity;
import io.renren.modules.photography.service.PWorksService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 作品展示
 *
 * @author gjun
 * @email 1372352966@qq.com
 * @date 2021-03-27 10:04:08
 */
@RestController
@RequestMapping("generator/pworks")
public class PWorksController {
    @Autowired
    private PWorksService pWorksService;

    @Value("${files.memberPhotoPath}")
    private String articlePath;
    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("generator:pworks:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pWorksService.queryPage(params);

        return R.ok().put("page", page);
    }


    @PostMapping("/upload")
    @RequiresPermissions("generator:pworks:all")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        return pWorksService.save(file,"memberPhotoPath",articlePath);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:pworks:info")
    public R info(@PathVariable("id") Integer id){
		PWorksEntity pWorks = pWorksService.getById(id);

        return R.ok().put("pWorks", pWorks);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:pworks:save")
    public R save(@RequestBody PWorksEntity pWorks){
		pWorksService.save(pWorks);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:pworks:update")
    public R update(@RequestBody PWorksEntity pWorks){
		pWorksService.updateById(pWorks);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:pworks:delete")
    public R delete(@RequestBody Integer[] ids){
		pWorksService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
